# This file contains the fastlane.tools configuration
# You can find the documentation at https://docs.fastlane.tools
#
# For a list of all available actions, check out
#
#     https://docs.fastlane.tools/actions
#
# For a list of all available plugins, check out
#
#     https://docs.fastlane.tools/plugins/available-plugins
#

# Uncomment the line if you want fastlane to automatically update itself
# update_fastlane

# A list of supported languages

ENV["LC_ALL"] = "de_DE.UTF-8"
ENV["LANG"] = "de_DE.UTF-8"

ENV["FASTLANE_SKIP_UPDATE_CHECK"] = "True"

default_platform(:ios)

platform :ios do

### PRE-LANE ACTIONS ############################

  before_all do
    setup_circle_ci

    if is_ci
      xcversion(version: "~> 13.0.0")
    end
  end

### LANES #######################################

  desc "Update license file"
  lane :update_licenses do
    update_third_party_notice()
  end

  desc "Build project"
  lane :build_for_testing do
    # build only, no tests
    run_tests(
      build_for_testing: true,
      scheme: "ENA",
      derived_data_path: "./DerivedData"
    )
  end

  desc "Build project (Community)"
  lane :build_community do
    build_ios_app(
      skip_codesigning: true,
      export_method: "ad-hoc",
      skip_package_ipa: true,
      destination: "platform=iOS Simulator,OS=15.0,name=iPhone 11",
      scheme: "ENACommunity"
    )
  end

  desc "Lint code"
  lane :lint do
    swiftlint(
      executable: "swiftlint",
      mode: :lint,
      output_file: "swiftlint.html",
      config_file: "ENA/.swiftlint.yml",
      strict: true,
      quiet: true,
      no_cache: false,
      reporter: "html"
    )
  end

  desc "Static code analysis via swiftlint's experimental feature"
  lane :analyze do
    UI.important "This lane uses experimental features and takes very long to complete!"
    UI.message "Better get a coffee… ☕️"

    build_ios_app(
      skip_codesigning: true,
      export_method: "ad-hoc",
      skip_package_ipa: true,
      destination: "platform=iOS Simulator,OS=15.0,name=iPhone 11",
      scheme: "ENA",
      buildlog_path: "DerivedData/logs/"
    )
    swiftlint(
      executable: "../../vendor/swiftlint",
      mode: :analyze,
      compiler_log_path: "DerivedData/logs/ENA-ENA.log",
      output_file: "swiftlint.analyze",
      config_file: "ENA/.swiftlint.yml",
      strict: false,
      quiet: false,
      no_cache: true,
      reporter: "xcode"
    )
  end

  desc "Run tests"
  lane :test do | options |
    begin
      # Execute tests for given test plan
      testPlan = options[:testplan] || "AllTests"

      run_tests(
        scheme: "ENA",
        derived_data_path: "./DerivedData",
        test_without_building: false, #`build_for_testing` is required if this is set to `true`!
        xcargs: "SWIFT_TREAT_WARNINGS_AS_ERRORS=NO",
        testplan: testPlan,
        only_test_configurations: ["DE"],
        devices: ["iPhone 11"],
        prelaunch_simulator: true,
        result_bundle: true,
        code_coverage: true,
        output_style: "raw", # prevents long periods of silence which makes the CI & devs nervous
        output_types: ""
      )
    rescue
      UI.user_error!("Tests did fail, please check logs above or ENA.xcresult.zip")
    ensure
      trainer(
        output_directory: "fastlane/test_output/",
        path: "fastlane/test_output/",
        extension: ".junit",
        fail_build: false
      )

      zip(
        path: "fastlane/test_output/ENA.xcresult",
        output_path: "fastlane/test_output/ENA.xcresult.zip",
        verbose: false
      )
    end
  end

  desc "Run smoke tests (Community)"
  lane :test_community do
    begin
      run_tests(
        scheme: "ENACommunity",
        xcargs: "SWIFT_TREAT_WARNINGS_AS_ERRORS=NO",
        testplan: "SmokeTests",
        concurrent_workers: 6,
        max_concurrent_simulators: 4,
        result_bundle: true,
        code_coverage: false,
        fail_build: false,
        output_types: "junit",
        output_files: "report_community.junit"
      )
    rescue
      UI.user_error!("Tests did fail, please check logs above or ENA.xcresult.zip")
    ensure
      trainer(
        output_directory: "fastlane/test_output/",
        path: "fastlane/test_output/",
        extension: ".junit",
        fail_build: false
      )

      zip(
        path: "fastlane/test_output/ENACommunity.xcresult",
        output_path: "fastlane/test_output/ENACommunity.xcresult.zip",
        verbose: false
      )
    end
  end

  desc "Create (localized) screenshots"
  lane :screenshot do |options|
    begin
      output_dir = "screenshots/screenshots-#{options[:languages]}-#{options[:mode]}"

      capture_ios_screenshots(
        languages:  options[:languages],
        dark_mode: options[:mode] == "dark",
        output_directory: output_dir,
        result_bundle: true
      )
    rescue
      UI.user_error!("Tests did fail, please check logs above or ENA.xcresult.zip")
    ensure
      trainer(
        output_directory: "fastlane/test_output/#{options[:languages]}-#{options[:mode]}",
        path: "#{output_dir}/test_output/#{options[:languages]}",
        extension: ".junit",
        fail_build: false
      )
      # no xcresult created, so no zip like in the other lanes
    end
  end

  desc "Build and upload for testing"
  lane :betaRelease do
    match(
      type: "appstore"
    )

    build_ios_app(
      scheme: "ENA",
      configuration: "TestFlight",
      clean: true
    )

    upload_to_testflight(
      username: "cwa.technical-user@sap.com",
      apple_id: "1515713549",
      skip_waiting_for_build_processing: true,
      skip_submission: true,  # only upload
    )
  end

  desc "Ad hoc distribution"
  lane :adHocDistribution do
    match(
      type: "adhoc",
      app_identifier: "de.rki.coronawarnapp-dev",
      template_name: "Exposure Notification for 523TP53AQF (Distribution) iOS Dist ADHOC"
    )

    build_ios_app(
      scheme: "ENA",
      configuration: "AdHoc",
      export_method: "ad-hoc",
      clean: true
    )

    # Upload to Mobile Device Cloud
    sh("curl \
      --location \
      --request POST '#{ENV["TSI_CLOUD_URL"]}' \
      --header 'Authorization: Bearer #{ENV["TSI_CLOUD_TOKEN"]}' \
      --form 'file=@../ENA.ipa'"
    )

    appcenter_upload(
      api_token: ENV["MS_APP_CENTER_TOKEN"],
      owner_name: "Corona-Warn-App",
      owner_type: "organization",
      app_name: "Corona-Warn-App",
      file: "ENA.ipa"
    )
  end

  desc "Update GitHub Pages"
  lane :updateDocs do

    # Create docs
    jazzy(
     config: ".jazzy.yaml"
    )

    # Deploy to gh pages
    sh("npm install -g --silent gh-pages@2.0.1")
    sh("gh-pages --message \"[ci skip] Update docs\" --dist ../../../docs/swift-doc")

  end

end
